//
//  NewsSongsViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/18.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "NewsSongsViewController.h"

@interface NewsSongsViewController ()

@end

@implementation NewsSongsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    CGFloat title_width = SCREEN_WIDTH*0.533333; // 200
    CGFloat title_height = SCREEN_WIDTH*0.08; // 30
    
    self.TitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, title_width, title_height)];
    self.TitleView.textColor = [UIColor whiteColor];
    self.TitleView.textAlignment = NSTextAlignmentCenter;
    self.TitleView.text = @"新歌首发";
    self.navigationItem.titleView = self.TitleView;
    
    _dataArr = [[NSMutableArray alloc] init];
    
    
    CGFloat flow_width = SCREEN_WIDTH*0.293333; // 110
    CGFloat flow_heigth = 140*ADJUSTWIDTH; // 120
    
    _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.itemSize = CGSizeMake(flow_width, flow_heigth);
    _flowLayout.minimumLineSpacing = 10;
    _flowLayout.minimumInteritemSpacing = 1;
    _flowLayout.sectionInset = UIEdgeInsetsMake(10,10,10,10);
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:_flowLayout];
    _collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_collectionView];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView registerClass:[ListCollectionViewCell class] forCellWithReuseIdentifier:@"NEWSONGS"];
    
    
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.removeFromSuperViewOnHide = YES;
    HUD.labelText = @"loading...";

    [self.view addSubview:HUD];
    HUD.mode = MBProgressHUDModeIndeterminate;
    
    [self getDataForMusicNewSongs];
    

    
    
    // Do any additional setup after loading the view.
}



- (void)getDataForMusicNewSongs
{
    
    
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    NSString *url_string = ListNewMusic;
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/x-javascript",nil];
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        NSArray *dicArray = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in dicArray) {
            
            RecommedModel *recommedModel = [[RecommedModel alloc] init];
            
            [recommedModel setValuesForKeysWithDictionary:dic];
            
            [_dataArr addObject:recommedModel];
        }
        
        HUD.hidden = YES;
        [_collectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _dataArr.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseID = @"NEWSONGS";
    ListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
    
    RecommedModel *model = [_dataArr objectAtIndex:indexPath.row];
    
    [cell getModelForCell:model];
    
    return cell;
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NewAlbumViewController *newVc = [[NewAlbumViewController alloc] init];
    
    RecommedModel *recommed = [_dataArr objectAtIndex:indexPath.row];
    
    newVc.recommedModel = recommed;
    
    [self.navigationController pushViewController:newVc animated:YES];
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
